﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace THO7AlgoritmTimerApplication {
    class MedianAlgorithmVar : VisionAlgorithm {
        private int mask;
        public MedianAlgorithmVar(String name) : base(name) {
            
        }
        public override Bitmap DoAlgorithm(Bitmap sourceImage) {
            mask = (int)getMarge();
            Bitmap dest = new Bitmap(sourceImage.Width, sourceImage.Height);
            BitmapData bmpdata = sourceImage.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            BitmapData bmpdata1 = dest.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
            IntPtr ptr = bmpdata.Scan0;
            IntPtr ptr1 = bmpdata1.Scan0;
            int regellengte = Math.Abs(bmpdata.Stride);
            int bytes = regellengte * sourceImage.Height;
            byte[] rgbValues = new byte[bytes];
            byte[] output = new byte[bytes];
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

            for (int hoogte = 0; hoogte < sourceImage.Height; hoogte++) {
                for (int breedte = 0; breedte < regellengte; breedte += 3) {
                    int pixel = (hoogte * regellengte) + breedte;
                    byte kleur = (byte)((rgbValues[pixel] + rgbValues[pixel + 1] + rgbValues[pixel + 2]) / 3);
                    rgbValues[pixel] = (kleur);
                    rgbValues[++pixel] = (kleur);
                    rgbValues[++pixel] = (kleur);
                }
            }
            int new_mask = (regellengte * ((mask / 2) + 1)) + (((mask / 2) + 1) * 3);
            byte waarde = 0;
            for (int regelnummer = 0; regelnummer < sourceImage.Height - ((mask/2)+1)*2; regelnummer++) {
                for (int convolutie = 0; convolutie < regellengte - ((mask/2)+1)*6; convolutie += 3) {
                    int begin_pixel = ((regelnummer * regellengte) + convolutie);
                    byte[] cumu_hist = new byte[256];
                    for (int hoogte = 0; hoogte < mask; hoogte++) {
                        for (int breedte = 0; breedte < mask; breedte++) {
                            int pixel = begin_pixel + (hoogte * regellengte) + (breedte * 3);
                            cumu_hist[rgbValues[pixel]]++;
                        }
                    }
                    for (byte i = 1; i < 255; i++) {
                        cumu_hist[i] += cumu_hist[i - 1];
                        if (cumu_hist[i] > (((mask*mask)/2)+1)) {
                            waarde = i;
                            break;
                        }
                    }
                    output[begin_pixel + new_mask] = (waarde);
                    output[begin_pixel + 1 + new_mask] = (waarde);
                    output[begin_pixel + 2 + new_mask] = (waarde);
                }
            }
            System.Runtime.InteropServices.Marshal.Copy(output, 0, ptr1, bytes);
            sourceImage.UnlockBits(bmpdata);
            dest.UnlockBits(bmpdata1);
            return dest;
        }
    }
}
